package com.sky.common.utils;

/**
 * edgeId和deviceId工具类
 *
 */
public class GeneratorUtil {

    /**
     * 边缘中心ID校验系数
     */
    private static final int[] EDGE_ID_FACTOR = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9};

    /**
     * 设备ID校验系数
     */
    private static final int[] DEVICE_ID_FACTOR = {4, 8, 3, 7, 5, 6, 3, 0, 8, 9};


    /**
     * 边缘中心ID前缀
     */
    public static final String EDGE_ID_PREFIX = "142184";

    /**
     * 生成边缘中心id
     * @param num 序列号
     * @return 边缘中心id
     */
    public static String generateEdgeId(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("序列号小于0");
        }
        if(num>999999){
            throw new IllegalArgumentException("序列号超过6位数");
        }
        String random = StringUtils.fixedLengthFormat(num, 6);
        StringBuilder result = new StringBuilder();
        result.append(EDGE_ID_PREFIX);
        result.append(random);
        int sum = 0;
        for (int i = 0; i < result.length(); i++) {
            int n = result.charAt(i) - '0';
            sum += n * EDGE_ID_FACTOR[i];
        }
        int mode = sum % 10000;
        result.append(StringUtils.fixedLengthFormat(mode, 4));
        return result.toString();
    }

    /**
     * 判断边缘中心id是否合法
     *
     * @param edgeId 边缘中心id
     * @return 是否合法
     */
    public static boolean isValidEdgeId(String edgeId) {
        if (edgeId == null || edgeId.length() != 16 || !edgeId.startsWith(EDGE_ID_PREFIX)) {
            return false;
        }
        int sum = 0;
        for (int i = 0; i < 12; i++) {
            int n = edgeId.charAt(i) - '0';
            sum += n * EDGE_ID_FACTOR[i];
        }
        String verity = edgeId.substring(12);
        String rightVerity = StringUtils.fixedLengthFormat(sum % 10000, 4);
        return verity.equals(rightVerity);
    }

    /**
     * 生成deviceId
     *
     * @param edgeId 边缘中心id
     * @return deviceId
     */
    public static String generateDeviceId(String edgeId) {
        if (edgeId == null || edgeId.length() != 16) {
            throw new IllegalArgumentException("edgeId:" + edgeId);
        }
        //生成10位随机数
        String random = StringUtils.fixedLengthFormat(RandomUtil.randomInt(10), 10);
        int sum = 0;
        for (int i = 0; i < random.length(); i++) {
            int n = random.charAt(i) - '0';
            sum += n * DEVICE_ID_FACTOR[i];
        }
        int mod = sum % 1000000;
        String modeStr = StringUtils.fixedLengthFormat(mod, 6);
        StringBuilder result = new StringBuilder();
        result.append(edgeId);
        result.append(random);
        result.append(modeStr);
        //6为校验码
        return result.toString();
    }

    /**
     * 校验
     *
     * @param deviceId 设备id
     * @return 是否合法
     */
    public static boolean isValidDeviceId(String deviceId) {
        if (deviceId == null || deviceId.length() != 32) {
            return false;
        }
        String edgeId = deviceId.substring(0, 16);
        //边缘中心id是否合法
        if (!isValidEdgeId(edgeId)) {
            return false;
        }
        String verity = deviceId.substring(26);
        String random = deviceId.substring(16, 26);
        int sum = 0;
        for (int i = 0; i < random.length(); i++) {
            int n = random.charAt(i) - '0';
            sum += n * DEVICE_ID_FACTOR[i];
        }
        int mod = sum % 1000000;
        String modeStr = StringUtils.fixedLengthFormat(mod, 6);
        return verity.equals(modeStr);
    }
}
